package com.atguigu.gmall.activity.controller;

import com.atguigu.gmall.activity.service.SeckillBizService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.order.model.OrderInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-08-16 10:51
 */
@RestController
@RequestMapping("/api/activity/seckill/auth")
public class SeckilBizController {


    @Autowired
    private SeckillBizService seckillBizService;


    /**
     * 获取当前用户秒杀商品抢购码
     *
     * @param skuId
     * @return
     */
    @GetMapping("/getSeckillSkuIdStr/{skuId}")
    public Result getSeckillSkuIdStr(HttpServletRequest request, @PathVariable("skuId") Long skuId) {
        //1.获取当前登录用户ID
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isBlank(userId)) {
            throw new RuntimeException("请先登录");
        }
        //2.调用业务逻辑生成用户抢购码
        String buyCode = seckillBizService.getSeckillBuyCode(userId, skuId);
        return Result.ok(buyCode);
    }


    /**
     * 秒杀请求入队（将秒杀请求放入消息队列RabbitMQ）；后续秒杀结果页面中定时器查询秒杀结果
     *
     * @param skuId
     * @param buyCode
     * @return
     */
    @PostMapping("/seckillOrder/{skuId}")
    public Result seckillOrderToQueue(HttpServletRequest request, @PathVariable("skuId") Long skuId, @RequestParam("skuIdStr") String buyCode) {
        //1.获取当前登录用户ID
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isBlank(userId)) {
            throw new RuntimeException("请先登录");
        }
        //2.调用业务逻辑进行秒杀入队处理
        seckillBizService.seckillOrderToQueue(userId, skuId, buyCode);
        return Result.ok();
    }


    /**
     * 前端定时轮询用户秒杀结果
     *
     * @param skuId 秒杀商品ID
     * @return
     */
    @GetMapping("/checkOrder/{skuId}")
    public Result checkSeckillResult(HttpServletRequest request, @PathVariable("skuId") Long skuId) {
        //1.获取当前登录用户ID
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isBlank(userId)) {
            throw new RuntimeException("请先登录");
        }
        //2.调用业务层获取当前用户秒杀结果
        return seckillBizService.checkSeckillResult(userId, skuId);
    }


    /**
     * 汇总秒杀订单确认页面数据汇总
     *
     * @param userId 用户ID
     * @param skuId  秒杀商品ID
     * @return
     */
    @GetMapping("/trade/{skuId}")
    public Result getSeckillOrderTradeData(@RequestHeader("userId") String userId, @PathVariable("skuId") Long skuId) {
        Map<String, Object> mapResult = seckillBizService.getSeckillOrderTradeData(userId, skuId);
        return Result.ok(mapResult);
    }


    /**
     * 提交用户秒杀订单
     *
     * @param orderInfo
     * @return
     */
    @PostMapping("/submitOrder")
    public Result submitSeckillOrder(HttpServletRequest request, @RequestBody OrderInfo orderInfo) {
        //1.获取当前登录用户ID
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isBlank(userId)) {
            throw new RuntimeException("请先登录");
        }
        orderInfo.setUserId(Long.valueOf(userId));
        //2.调用业务逻辑保存秒杀订单
        Long orderId = seckillBizService.submitSeckillOrder(orderInfo);
        return Result.ok(orderId);
    }
}
